package com.jiankangjin.appspos.controller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jiankangjin.appspos.entity.info.GetNoticeInfoEntity;
import com.jiankangjin.appspos.service.NoticeServices;
import com.jiankangjin.appspos.util.CheckToken;
import com.jiankangjin.appspos.util.StringUtil;
import com.jiankangjin.appspos.entity.info.GetNoticeTipVo;
import com.jiankangjin.appspos.vo.GetUserInfoVo;

@Controller
public class NoticeController {
	private static final transient Log logger = LogFactory.getLog(NoticeController.class);
	
	@Autowired
	private NoticeServices noticeServices;
	
	/**
	 * 更新通知信息
	 */
	@RequestMapping(value="/get_notice_tip", method = RequestMethod.POST)
	@ResponseBody
	public GetNoticeTipVo getNoticeTip(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		sb.append("token=" + token);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("更新通知信息接口:" + sb);
		}
		GetNoticeTipVo getNoticeTipVo = new GetNoticeTipVo();
		getNoticeTipVo.setResp_status("9999");
		if (StringUtil.isEmpty(token)) {
			getNoticeTipVo.setMsg("token不能为空");
			return getNoticeTipVo;
		}
		if (StringUtil.isEmpty(sign)) {
			getNoticeTipVo.setMsg("sign不能为空");
			return getNoticeTipVo;
		}
		
		// Token验证
		GetUserInfoVo userInfo = null;
		userInfo = CheckToken.getUserInfo(token);
		if (!"0000".equals(userInfo.getResp_status())) {
			 getNoticeTipVo.setMsg("token 验证失败！");
			 return getNoticeTipVo;
		}
		
		return noticeServices.getNoticeTip(token);
	}
	
	/**
	 * 更新通告信息 
	 */
	@RequestMapping(value="/get_notice_info", method = RequestMethod.POST)
	@ResponseBody
	public GetNoticeInfoEntity getNoticeInfo(
			@RequestParam(required = false) String token,
			@RequestParam(required = false) String notice_type,
			@RequestParam(required = false) String time_stamp,
			@RequestParam(required = false) String quantity,
			@RequestParam(required = false) String sign) {
		StringBuilder sb = new StringBuilder();
		logger.info("token=" + token);
		sb.append(" notice_type=" + notice_type);
		sb.append(" time_stamp=" + time_stamp);
		sb.append(" quantity=" + quantity);
		sb.append(" sign=" + sign);
		if (logger.isInfoEnabled()) {
			logger.info("更新通告信息接口:" + sb);
		}
		GetNoticeInfoEntity getNoticeInfoEntity = new GetNoticeInfoEntity();
		getNoticeInfoEntity.setResp_status("9999");
		if (token == null || token.equals("")) {
			getNoticeInfoEntity.setMsg("token不能为空");
		}
		Integer _notice_type = 0;
		Integer _quantity = 10;
		if (StringUtil.isEmpty(notice_type)) {
			getNoticeInfoEntity.setMsg("notice_type不能为空");
			return getNoticeInfoEntity;
		} else {
			if (StringUtil.isNotNum(notice_type)) {
				getNoticeInfoEntity.setMsg("notice_type必须是数字");
				return getNoticeInfoEntity;
			} else {
				_notice_type = Integer.parseInt(notice_type);
			}
		}
		if (StringUtil.isNotEmpty(quantity)) {
			if (StringUtil.isNotNum(quantity)) {
				getNoticeInfoEntity.setMsg("quantity必须是数字");
				return getNoticeInfoEntity;
			} else {
				_quantity = Integer.parseInt(quantity);
			}
		}
		if (StringUtil.isEmpty(sign)) {
			getNoticeInfoEntity.setMsg("sign不能为空");
			return getNoticeInfoEntity;
		}
		
		// Token验证
		GetUserInfoVo userInfo = null;
		userInfo = CheckToken.getUserInfo(token);
		if (!"0000".equals(userInfo.getResp_status())) {
			getNoticeInfoEntity.setMsg("token 验证失败！");
			return getNoticeInfoEntity;
		}
		
		return noticeServices.getNoticeInfo( _notice_type, time_stamp, _quantity);
	}
}
